<?php
include_once("../configure/admin.config.inc.php");//--> admin global var
include_once("db.inc.php");						//--> db global var
include_once("Operation.php");					//--> Operation
include_once("Product.php");					//--> Product
include_once("UploadFiles.php");					//--> UploadFiles
include_once("Files.php");							//--> Files
require_once("controlHeader.php");				//--> system control header

$objOperate		= new Operation($objSession->getLanguage());		//--> Operation instance
$objProduct 	= new Product(DB_TAG_PUBLIC, $uid);					//--> Product
$objFiles		= new Files(DB_TAG_PUBLIC, $uid);					//--> Files
$objUploadFiles	= new UploadFiles(DB_TAG_PUBLIC, $uid);				//--> UploadFiles

$arrOperate = $objOperate->arrGetFromGroupIDAndMenuID($gid, $menuid);

if(isset($_GET['id']) && !empty($_GET['id'])){
	$id = $_GET['id'];

	$isReturn = $objProduct->delete($arrOperate, $id, $LANGEUAGE_ID);
	
	if($isReturn){
		$objProduct->deleteRelated($arrOperate, $id);
		
		$arrImages = $objProduct->getHash(DB_TAG_PUBLIC, "SELECT u.id, u.extName FROM upload_files u, product_images p WHERE u.id = p.imageID AND p.productID = $id AND type = " . UploadFiles::TYPE_IMAGE);
		foreach($arrImages as $imageid => $extName){
			$objProduct->deleteImage($arrOperate, $id, $imageid);
			
			$filepath = PRODUCT_IMAGE_DIR . $objFiles->getDirByID($imageid) . '/' . $imageid . '.' . $extName;
			
			if(file_exists($filepath)){
				unlink($filepath);
				
				$objUploadFiles->delete($arrOperate, $imageid);
				echo 'delete picture success.<br>';
			}

		}
		
		
		$error_message = 'delete successfully.';
	}else 
		$error_message = 'delete failure.';
	
}


echo "<script language='javascript'>";
echo "alert(\"$error_message\");";
echo "location.href=\"".$_SERVER['HTTP_REFERER']."\";";
echo "</script>";

//$backurl = $_SERVER['HTTP_REFERER'];
//header("Location: $backurl");
?>
